package org.inspursc.s0517.health.evaluation.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.inspursc.s0517.health.evaluation.converter.QuestionConverter;
import org.inspursc.s0517.health.evaluation.dao.QuestionDao;
import org.inspursc.s0517.health.evaluation.dto.QuestionDTO;
import org.inspursc.s0517.health.evaluation.entity.QuRadio;
import org.inspursc.s0517.health.evaluation.entity.Question;
import org.inspursc.s0517.health.evaluation.enums.QuTypeEnum;
import org.inspursc.s0517.health.evaluation.service.QuRadioService;
import org.inspursc.s0517.health.evaluation.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author B_Dream
 * @date 2018/5/23 10:48
 */
@Service
@Slf4j
public class QuestionServiceImpl implements QuestionService {

    @Autowired
    QuRadioService quRadioService;

    @Autowired
    private QuestionDao questionDao;

    @Override
    @Transactional
    public QuestionDTO saveQuestion(QuestionDTO question) {

        Question entity = QuestionConverter.converter(question);
        if (question.getId() == null) {

            //保存题目的题干部分
            Question newEntity = questionDao.save(entity);
            QuestionDTO questionDTO = QuestionConverter.converters(newEntity);
            entity.setId(newEntity.getId());

            //判断题目类型
            QuTypeEnum quTypeEnum = entity.getQuTypeEnum();

            List<QuRadio> quRadios = entity.getQuRadios();
            quRadios.forEach(e -> e.setQuId(newEntity.getId()));

            if (quTypeEnum == QuTypeEnum.RADIO || quTypeEnum == QuTypeEnum.COMPRADIO) {
                quRadios = quRadioService.saveBatchRadio(quRadios);
            }

            questionDTO.setQuRadios(quRadios);
            return questionDTO;
        } else {

            Question newEntity = null;
            List<QuRadio> quRadios = null;
            try {
                newEntity = questionDao.save(entity);
                quRadioService.deleteByquId(newEntity.getId());
                Integer quId = newEntity.getId();
                QuTypeEnum quTypeEnum = entity.getQuTypeEnum();
                entity.getQuRadios().forEach(e -> e.setQuId(quId));
                if (quTypeEnum == QuTypeEnum.RADIO || quTypeEnum == QuTypeEnum.COMPRADIO) {
                    quRadios = quRadioService.saveBatchRadio(entity.getQuRadios());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            QuestionDTO dtos = QuestionConverter.converters(newEntity);
            dtos.setQuRadios(quRadios);
            return dtos;
        }
    }

    @Override
    public List<QuestionDTO> listQuestionNoRadio() {
        List<Question> questions = questionDao.findAll();
        List<QuestionDTO> dtos = null;
        dtos = questions.stream().map(QuestionConverter::converters).collect(Collectors.toList());
        return dtos;
    }


    @Override
    public List<QuestionDTO> listQuestions(Integer belongId) {
        List<Question> questions = questionDao.findByCategoryId(belongId);
        questions.parallelStream().forEach(this::getQuestionOption);
        List<QuestionDTO> dtos = null;
        dtos = questions.stream().map(QuestionConverter::converters).collect(Collectors.toList());
        return dtos;
    }


    @Override
    public List<QuestionDTO> listQuestionsById(List<Integer> quIds) {
        List<Question> questions = questionDao.findAllById(quIds);
        questions.parallelStream().forEach(this::getQuestionOption);
        List<QuestionDTO> dtos = null;
        dtos = questions.stream().map(QuestionConverter::converters).collect(Collectors.toList());
        return dtos;
    }

    @Override
    public void getQuestionOption(Question question) {

        Integer quId = question.getId();
        QuTypeEnum quTypeEnum = question.getQuTypeEnum();
        if (quTypeEnum == QuTypeEnum.RADIO || quTypeEnum == QuTypeEnum.COMPRADIO) {
            question.setQuRadios(quRadioService.findByQuId(quId));
        }
    }

    @Override
    @Transactional
    public boolean deletesById(Integer id) throws Exception {

        if (id != null && !"".equals(id)) {
            try {
                questionDao.deleteById(id);
                List<QuRadio> quRadios = quRadioService.findByQuId(id);
                quRadioService.deleteBatch(quRadios);
                return true;
            } catch (Exception e) {
                return false;
            }
        } else {
            throw new Exception("id为空");
        }
    }


}
